package com.liyuan.yiqixing.service.impl;

import com.liyuan.yiqixing.model.Page;
import com.liyuan.yiqixing.service.PageService;
import org.springframework.stereotype.Service;

import java.util.LinkedList;

@Service
public class PageServiceImpl implements PageService {
    @Override
    public Page paging(Integer currentPage, int scale, int sum) {
        Page page = new Page();
        page.setCurrentPage(currentPage);
        int lastPage = sum/scale + (sum%scale==0?0:1);
        page.setLastPage(lastPage);
        LinkedList<Integer> list = new LinkedList<>();
        list.add(currentPage);
        for(int i=1;i<scale/2;i++){
            if(currentPage-i>0){
                list.addFirst(currentPage-i);
            }if(currentPage+i<=lastPage){
                list.addLast(currentPage+i);
            }
        }
        Integer[]pages = new Integer[list.size()];
        for(int i=0;i<pages.length;i++){
            pages[i] = list.get(i);
        }
        page.setPages(pages);
        return page;
    }
}
